// pages/location/location.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    code: "",
    code1: ""    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getPhone(event){
    this.setData({phone: event.detail.value});
  },
  codeRequire(event){
    let phone = this.data.phone;
    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    let code = "";
    if(phone){
      if(phone.length < 11){
        wx.showToast({
          title: '手机号长度错误',
          icon: "error",
          duration: 2000
        })
      }
      else if(!myreg.test(phone)){
        wx.showToast({
          title: '手机号格式错误',
          icon: "error",
          duration: 2000
        })
      }else{
        code = Math.floor(Math.random() * (9999 - 1000 + 1) + 1000);
        wx.showToast({
          title: '验证码为' + code,
          icon: "success",
          duration: 2000
        })
        this.setData({code: code});
      }
    }else{
      wx.showToast({
        title: '手机号不得为空',
        duration: 2000,
        icon: "error",
      })
    }
  },
  codeVerify(event){
    this.setData({code1: event.detail.value});
  },
  LoginVerify(){
    const code1 = this.data.code1;
    const code = this.data.code;
    const phone = this.data.phone;
    if(phone && code1){
      if(code){
        if(code != code1){
          wx.showToast({
            title: '验证码错误',
            duration: 2000,
            icon: "error",
          })
          this.setData({code: ""});
        }else{
          wx.showToast({
            title: '登录成功',
            duration: 2000,
            icon: "success",
          })
          app.globalData.UserInfo = {
            phone: this.data.phone,
          }
          console.log(app.globalData.UserInfo);
          wx.switchTab({
            url: '../home/home',
          })
        }
      }else{
        wx.showToast({
          title: '验证码不得为空',
          duration: 2000,
          icon: "error",
        })
      }
    }else{
      wx.showToast({
        title: '手机号不得为空',
        duration: 2000,
        icon: "error",
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})